import { request} from '@umijs/max';


export enum Method {
  GET = 'GET',
  POST = 'POST',
  PUT = 'PUT',
  DELETE = 'DELETE',
  PATCH = 'PATCH',
}
interface RestResponse<T = any>  {
  code: number;
  data: T;
  msg: string;
  total?: number;
  success?: boolean;
  next?: boolean;
  skip?: number;
  tradeId?: string;
}


export async function httpRequest<T>(url: string, params?: any, payload?: any, options?: any) : Promise<RestResponse<T>> {
  // @ts-ignore
  return request<RestResponse<T>>(url,{data: payload, params: params, ...options});
}

export function httpGet<T = any>(url: string, param?: any, options?: any): Promise<RestResponse<T>> {
  return httpRequest<T>(url, param, null, {method: Method.GET, ...options,} as any);
}

/**
 * 分页请求兼容GET
 */
export function httpPageableGet<T = any>(url: string, param?: any, options?: any): Promise<RestResponse<T>> {

  const {current, pageSize, ...p} = param;
  return httpRequest<T>(url, {
    page: (current || 1) ,
    size: pageSize, ...p
  }, null, {method: Method.GET, ...options,} as any);
}


export function httpPost<T = any>(url: string, data?: any, options?: any): Promise<RestResponse<T>> {
  return httpRequest<T>(url, null, data, {method: Method.POST, ...options,} as any);
}

/**
 * 分页请求兼容POST
 */
export function httpPageablePost<T = any>(url: string, param?: any, options?: any): Promise<RestResponse<T>> {
  const {current, pageSize, ...p} = param;

  return httpRequest<T>(url, {
    page: (current || 1),
    size: pageSize, ...p
  }, param, {method: Method.POST, ...options,} as any);
}


export function httpPostWithQuery<T = any>(url: string, param?: any, data?: any, options?: any): Promise<RestResponse<T>> {
  return httpRequest<T>(url, param, data, {method: Method.POST, ...options,} as any);
}

export function httpPut<T>(url: string, data?: any, options?: any): Promise<RestResponse<T>> {
  return httpRequest<T>(url, null, data, {method: Method.PUT, ...options});
}

export function httpPatch<T = any>(url: string, data?: any, options?: any): Promise<RestResponse<T>> {
  return httpRequest<T>(url, null, data, {method: Method.PATCH, ...options,} as any);
}

export function httpDelete<T>(url: string, data?: any): Promise<RestResponse<T>> {
  return httpRequest<T>(url, null, data, {method: Method.DELETE});
}


